package com.lianjie.address.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class LinuxUtil {

    public static String run(String cmd) {
        System.out.println("执行命令[ " + cmd + "]");
        Runtime run = Runtime.getRuntime();
        try {
            Process process = run.exec(cmd);
            String line;
            BufferedReader stdoutReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            StringBuffer out = new StringBuffer();
            while ((line = stdoutReader.readLine()) != null) {
                out.append(line);
            }
            try {
                process.waitFor();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            process.destroy();
            return out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String executeLinuxCmd(String command){
        try {
            Process process = Runtime.getRuntime().exec(command);
            InputStreamReader rd = new InputStreamReader(process.getInputStream());
            BufferedReader input = new BufferedReader(rd);
            String ski = input.lines().findAny().get();
            System.out.println(ski);
            String addrInt = input.lines().findAny().get();
            System.out.println(addrInt);
            String addr = input.lines().findAny().get();
            System.out.println(addr);
            return  addr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
